package com.test.applet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {
	public static final int PORT = 2000;
	// declare local variables
	ServerSocket echoServer = null;
	Socket clientSocket = null;
	BufferedReader in;
	PrintWriter out;
	String s;

	public SocketServer() {
		// fire up the server, catching IOException in case of problems
		try {
			echoServer = new ServerSocket(PORT);
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		if (echoServer != null) {
			System.out.println("Server listening on port " + PORT);
		}
		// create a new socket for incoming transactions and Streams
		// to handle data
		try {
			clientSocket = echoServer.accept();
			in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
			out = new PrintWriter(clientSocket.getOutputStream());
			// if socket created, print out details
			while (true) {
				// System.out.println("Message Received From:" +
				// clientSocket.getInetAddress( ) + "\nFrom Port: " +
				clientSocket.getPort();
				// send input to output!
				s = in.readLine();
				System.out.println("I Hava Read the line:" + s);
				out.println(s);
				out.flush();
			}
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		System.out.println("Finished.");
	}

	public static void main(String[] args) {
		SocketServer ms = new SocketServer();
	}
}